package net.zjitc.controller;

import com.google.gson.Gson;
import net.zjitc.domain.*;
import net.zjitc.service.CartItemService;
import net.zjitc.service.CategoryService;
import net.zjitc.service.ProductService;
import net.zjitc.service.SecondService;
import net.zjitc.service.impl.CartItemServiceImpl;
import net.zjitc.service.impl.CategoryServiceImpl;
import net.zjitc.service.impl.ProductServiceImpl;
import net.zjitc.service.impl.SecondServiceImpl;
import net.zjitc.utils.URLMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.util.List;

/**
 * 2 * @Author: 洪瑶
 * 3 * @Date: 2018/11/21 18:37
 * 4
 */
public class CartController {
    ProductService pservice = new ProductServiceImpl();
    CartItemService cservice = new CartItemServiceImpl();
    @URLMapping(url = "/addMyCart")
    public String addMyCart(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        User user = (User) request.getSession().getAttribute("user");
        if (user != null)
        {
            int id = Integer.parseInt(request.getParameter("pid"));
            Product product = pservice.findbyid(id);
            CartItem cartItem = new CartItem();
            cartItem.setProduct(product);
            cartItem.setUser(user);
            cartItem.setQuantity(Integer.parseInt(request.getParameter("quantity")));
            CartItem aleady = cservice.findById(user.getUid(),product.getPid());
            if (aleady == null)
            {
                cservice.addCart(cartItem);
            }else {
                int count = cartItem.getQuantity()+aleady.getQuantity();
                aleady.setQuantity(count);
                cservice.update(aleady);
            }
            return "/findMyCart";
        }else {
            request.setAttribute("msg","请先登录");
            return "/WEB-INF/user/login.jsp";
        }

    }

    @URLMapping(url = "/findMyCart")
    public String findMyCart(HttpServletRequest request,HttpServletResponse response) throws Exception {
        User user = (User) request.getSession().getAttribute("user");
        if (user != null)
        {
            List<CartItem>list = cservice.findByuid(user.getUid());
            for (CartItem c :list
                    ) {
                Product p = pservice.findbyid(c.getPid());
                c.setProduct(p);
            }
            request.setAttribute("list",list);
            return "/WEB-INF/user/cart.jsp";
        }else {
            request.setAttribute("msg","请先登录");
            return "/WEB-INF/user/login.jsp";
        }
    }

    @URLMapping(url = "/deleteMyCart")
    public String deleteMyCart(HttpServletRequest request,HttpServletResponse response) throws SQLException {
        int cartid = Integer.parseInt(request.getParameter("cartid"));
        cservice.delete(cartid);
        return "/findMyCart";
    }

    @URLMapping(url = "/cleanMyCart")
    public String cleanMyCart(HttpServletRequest request,HttpServletResponse response) throws SQLException {
        User user = (User) request.getSession().getAttribute("user");
        cservice.deleteAll(user.getUid());
        return "/findMyCart";
    }
    @URLMapping(url = "/updateMyCart")
    public void updateMyCart(HttpServletRequest request,HttpServletResponse response)throws Exception{
//        CartItem cartItem = new Gson().fromJson(request.getReader(),CartItem.class);
    }
}
